define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dahuinew/dljzmonthone/index',
                    add_url: 'dahuinew/dljzmonthone/add',
                    edit_url: 'dahuinew/dljzmonthone/edit',
                    del_url: 'dahuinew/dljzmonthone/del',
                    multi_url: 'dahuinew/dljzmonthone/multi',
                    table: 'dahuinew_dljzmonthone',
                }
            });

            var table = $("#table");

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["100%", "100%"]);
            $(".btn-audit").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
                $(".btn-auditone").data("area", ["100%", "100%"]);
            });

            table.on('load-success.bs.table', function (e, data) {
                $("#money").text(data.extend.money);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                search: false,
                sortName: 'id',
                searchFormVisible: true,
                dblClickToEdit: false, //是否启用双击编辑
                //showExport: false,
                exportTypes: ['csv'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false, sortable: true},
                        //{field: 'Dljzywone_id', title: __('Dljzywone_id')},
                        //{field: 'sbzt', title: __('Sbzt'), searchList: {"未办理":__('未办理'),"已办理":__('已办理')}, formatter: Controller.api.formatter.sbstatus},
                        //{field: 'zczt', title: __('Zczt'), searchList: {"未办理":__('未办理'),"已办理":__('已办理')}, formatter: Controller.api.formatter.zcstatus},
                        {field: 'sbyf', title: __('Sbyf'),  defaultValue: Controller.api.defaultdate(), addclass:'datetimepicker'},
                        {field: 'dljzywone.htend2', title: __('合同停止'), operate:false},
                        {field: 'yje', title: __('月金额'), visible:Config.isadmin, operate:false},

                        {field: 'dljzywone.zhsl', title: __('综合税率(%)'), sortable: true},

                        {field: 'dljzywone.htend', title: __('Dljzywone.htend'), addclass:'datetimepicker'},
                        {field: 'dljzywone.zcyq', title: __('Dljzywone.zcyq'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符', formatter: Table.api.formatter.search},
                        {field: 'dljzywone.issh', title: __('是否上海'), searchList: {"上海":__('上海'),"非上海":__('非上海')}, formatter: Table.api.formatter.normal},

                        {field: 'dljzywone.gsname', title: __('Dljzywone.gsname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'dljzywone.shuiz', title: __('Dljzywone.shuiz'), searchList: {"小规模":__('小规模'),"一般纳税人":__('一般纳税人')}, formatter: Table.api.formatter.normal},
                        {field: 'dljzywone.faren', title: __('Dljzywone.faren'), operate:false},
                        {field: 'dljzywone.farentel', title: __('Dljzywone.farentel'), operate:false},
                        {field: 'dljzywone.farenidc', title: __('Dljzywone.farenidc'), operate:false},
                        {field: 'dljzywone.gssbpsw', title: __('Dljzywone.Gssbpsw'), operate:false},
                        {field: 'dljzywone.frswpsw', title: __('Dljzywone.Frswpsw'), operate:false},
                        {field: 'dljzywone.contact', title: __('Dljzywone.contact'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'dljzywone.contacttel', title: __('Dljzywone.contacttel'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'jbry', title: __('Jbry')},
                        {field: 'dljzywone.remarks1', title: __('Dljzywone.remarks1'), operate:false},
                        {field: 'dljzywone.remarks2', title: __('Dljzywone.remarks2'), operate:false},
                        //{
                        //    field: 'dljzywone.remarks1',
                        //    title: __('Dljzywone.remarks1'),
                        //    editable: {
                        //        emptytext: '无',
                        //        rows:2,
                        //        tpl: "<textarea rows='10' cols='50'></textarea>",
                        //        success: function (response, newValue) {
                        //            //$(".btn-refresh").trigger("click");
                        //        }
                        //    },
                        //    operate: false,
                        //},
                        //{
                        //    field: 'dljzywone.remarks2',
                        //    title: __('Dljzywone.remarks2'),
                        //    editable: {
                        //        emptytext: '无',
                        //        rows:2,
                        //        tpl: "<textarea rows='10' cols='50'></textarea>",
                        //        success: function (response, newValue) {
                        //            //$(".btn-refresh").trigger("click");
                        //        }
                        //    },
                        //    operate: false,
                        //},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'dljzywone.id', title: __('Dljzywone.id')},
                        //{field: 'dljzywone.htbh', title: __('Dljzywone.htbh')},
                        //{field: 'dljzywone.htstart', title: __('Dljzywone.htstart')},
                        //{field: 'dljzywone.htend', title: __('Dljzywone.htend')},
                        //{field: 'dljzywone.htje', title: __('Dljzywone.htje'), operate:'BETWEEN'},
                        //
                        //{field: 'dljzywone.gsname', title: __('Dljzywone.gsname')},
                        //{field: 'dljzywone.gssh', title: __('Dljzywone.gssh')},
                        //{field: 'dljzywone.khh', title: __('Dljzywone.khh')},
                        //{field: 'dljzywone.zhanghao', title: __('Dljzywone.zhanghao')},
                        //{field: 'dljzywone.shuiz', title: __('Dljzywone.shuiz')},
                        //{field: 'dljzywone.faren', title: __('Dljzywone.faren')},
                        //{field: 'dljzywone.farentel', title: __('Dljzywone.farentel')},
                        //{field: 'dljzywone.farenidc', title: __('Dljzywone.farenidc')},
                        //{field: 'dljzywone.contact', title: __('Dljzywone.contact')},
                        //{field: 'dljzywone.contacttel', title: __('Dljzywone.contacttel')},
                        //{field: 'dljzywone.gssbpsw', title: __('Dljzywone.gssbpsw')},
                        //{field: 'dljzywone.frswpsw', title: __('Dljzywone.frswpsw')},
                        //{field: 'dljzywone.kpyw', title: __('Dljzywone.kpyw')},
                        //{field: 'dljzywone.kpinfo', title: __('Dljzywone.kpinfo')},
                        //{field: 'dljzywone.jbry', title: __('Dljzywone.jbry')},
                        //{field: 'dljzywone.create_time', title: __('Dljzywone.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'dljzywone.update_time', title: __('Dljzywone.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            var form = $("form.form-commonsearch");
            if ($(".datetimepicker", form).size() > 0) {
                require(['bootstrap-datetimepicker'], function () {
                    var options = {
                        format: 'YYYY-MM',
                        icons: {
                            time: 'fa fa-clock-o',
                            date: 'fa fa-calendar',
                            up: 'fa fa-chevron-up',
                            down: 'fa fa-chevron-down',
                            previous: 'fa fa-chevron-left',
                            next: 'fa fa-chevron-right',
                            today: 'fa fa-history',
                            clear: 'fa fa-trash',
                            close: 'fa fa-remove'
                        },
                        showTodayButton: true,
                        showClose: true
                    };
                    $('.datetimepicker', form).parent().css('position', 'relative');
                    $('.datetimepicker', form).datetimepicker(options);
                });
            }

            //点击转出状态
            $(document).on("dblclick", ".zcstatus[data-id]", function () {
                if (Config.jingban) {
                    var url = 'dahuinew/dljzmonthone/changezcstatus';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret.data.zczt);
                        _this.html(ret.data.zczt);
                        if (ret.data.zczt != '已办理') {
                            _this.addClass('text-danger');
                        } else {
                            _this.removeClass('text-danger')
                        }
                    });
                }

            });

            //点击申报状态
            $(document).on("dblclick", ".sbstatus[data-id]", function () {
                if (Config.jingban) {
                    var url = 'dahuinew/dljzmonthone/changesbstatus';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret.data.sbzt);
                        _this.html(ret.data.sbzt);
                        if (ret.data.sbzt != '已办理') {
                            _this.addClass('text-danger');
                        } else {
                            _this.removeClass('text-danger')
                        }
                    });
                }

            });

            // 批量转出状态
            $(document).on("click", ".plzcstatus1", function () {
                if (Config.jingban) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahuinew/dljzmonthone/plzcstatus1?ids=' + ids;

                    var that = this;
                    Layer.confirm(__('批量转出状态 => "已办理"?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 批量转出状态
            $(document).on("click", ".plzcstatus0", function () {
                if (Config.jingban) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahuinew/dljzmonthone/plzcstatus0?ids=' + ids;

                    var that = this;
                    Layer.confirm(__('批量转出状态 => "未办理"?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 批量缴纳状态
            $(document).on("click", ".pljnstatus1", function () {
                if (Config.jingban) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahuinew/dljzmonthone/pljnstatus1?ids=' + ids;

                    var that = this;
                    Layer.confirm(__('批量申报状态 => "已办理"?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 批量缴纳状态
            $(document).on("click", ".pljnstatus0", function () {
                if (Config.jingban) {
                    var ids = Table.api.selectedids(table);
                    var url = 'dahuinew/dljzmonthone/pljnstatus0?ids=' + ids;

                    var that = this;
                    Layer.confirm(__('批量申报状态 => "未办理"?'), {icon: 3, title: '提示'}, function (index) {
                        Backend.api.ajax({
                            url: url,
                        }, function () {
                            Layer.close(index);
                            $(".btn-refresh").trigger("click");
                        });
                    });
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index2: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dahuinew/dljzmonthone/index2',
                    add_url: 'dahuinew/dljzmonthone/add',
                    edit_url: 'dahuinew/dljzmonthone/edit',
                    del_url: 'dahuinew/dljzmonthone/del',
                    multi_url: 'dahuinew/dljzmonthone/multi',
                    table: 'dahuinew_dljzmonthone',
                }
            });

            var table = $("#table");

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["100%", "100%"]);
            $(".btn-audit").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
                $(".btn-auditone").data("area", ["100%", "100%"]);
            });

            table.on('load-success.bs.table', function (e, data) {
                $("#money").text(data.extend.money);
            });


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                search: false,
                sortName: 'id',
                searchFormVisible: true,
                dblClickToEdit: false, //是否启用双击编辑
                //showExport: false,
                exportTypes: ['csv'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false, sortable: true},
                        {field: 'dljzywone.htbh', title: __('Dljzywone.htbh'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'jbry', title: __('Jbry')},
                        {field: 'dljzywone.htstart', title: __('Dljzywone.htstart'), addclass:'datetimepicker'},
                        {field: 'dljzywone.htend2', title: __('Dljzywone.htend'), addclass:'datetimepicker'},
                        //{field: 'sbyf', title: __('Sbyf'),  addclass:'datetimepicker'},
                        {field: 'dljzywone.htje', title: __('Dljzywone.htje'), operate:false},
                        {field: 'dljzywone.zcyq', title: __('Dljzywone.zcyq'), operate:false},
                        {field: 'dljzywone.gsname', title: __('Dljzywone.gsname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'dljzywone.gssh', title: __('Dljzywone.gssh'), operate:false},
                        {field: 'dljzywone.khh', title: __('Dljzywone.khh'), operate:false},
                        {field: 'dljzywone.zhanghao', title: __('Dljzywone.zhanghao'), operate:false},
                        {field: 'dljzywone.shuiz', title: __('Dljzywone.shuiz'), searchList: {"小规模":__('小规模'),"一般纳税人":__('一般纳税人')}, formatter: Table.api.formatter.normal},
                        {field: 'dljzywone.faren', title: __('Dljzywone.faren'), operate:false},
                        {field: 'dljzywone.farentel', title: __('Dljzywone.farentel'), operate:false},
                        {field: 'dljzywone.farenidc', title: __('Dljzywone.farenidc'), operate:false},
                        {field: 'dljzywone.contact', title: __('Dljzywone.contact'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'dljzywone.contacttel', title: __('Dljzywone.contacttel'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'dljzywone.gssbpsw', title: __('Dljzywone.gssbpsw'), operate:false},
                        {field: 'dljzywone.frswpsw', title: __('Dljzywone.frswpsw'), operate:false},
                        {field: 'dljzywone.kpyw', title: __('Dljzywone.kpyw'), searchList: {"包年":__('包年'),"否":__('否'),"包数量":__('包数量')}, formatter: Table.api.formatter.normal},
                        {field: 'dljzywone.kpinfo', title: __('Dljzywone.kpinfo'), operate:false},
                        {field: 'dljzywone.remarks1', title: __('Dljzywone.remarks1'), operate:false},
                        {field: 'dljzywone.remarks2', title: __('Dljzywone.remarks2'), operate:false},
                    ]
                ]
            });

            var form = $("form.form-commonsearch");
            if ($(".datetimepicker", form).size() > 0) {
                require(['bootstrap-datetimepicker'], function () {
                    var options = {
                        format: 'YYYY-MM',
                        icons: {
                            time: 'fa fa-clock-o',
                            date: 'fa fa-calendar',
                            up: 'fa fa-chevron-up',
                            down: 'fa fa-chevron-down',
                            previous: 'fa fa-chevron-left',
                            next: 'fa fa-chevron-right',
                            today: 'fa fa-history',
                            clear: 'fa fa-trash',
                            close: 'fa fa-remove'
                        },
                        showTodayButton: true,
                        showClose: true
                    };
                    $('.datetimepicker', form).parent().css('position', 'relative');
                    $('.datetimepicker', form).datetimepicker(options);
                });
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            defaultdate: function () {
                var date = new Date();
                var year = date.getFullYear();
                var month = date.getMonth() + 1;//获取当前月份(0-11,0代表1月)
                if (month >= 10) {
                    month = "" + month;
                } else {
                    month = "0" + month;
                }
                //console.log("QQQ---" + year + "-" + month);
                return year + "-" + month;
            },
            validateIdCard: function (idCard) {
                idCard = idCard.trim();
                var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;

                //如果通过该验证，说明身份证格式正确，但准确性还需计算
                if (regIdCard.test(idCard)) {
                    if (idCard.length == 18) {
                        var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
                        var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
                        var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
                        for (var i = 0; i < 17; i++) {
                            idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
                        }
                        var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
                        var idCardLast = idCard.substring(17);//得到最后一位身份证号码
                        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
                        if (idCardMod == 2) {
                            if (idCardLast == "X" || idCardLast == "x") {
                                //alert("恭喜通过验证啦！");
                                return true;
                            } else {
                                //alert("身份证号码错误！");
                                Backend.api.toastr.error("身份证号码验证错误,请修改！");
                                return false;
                            }
                        } else {
                            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                            if (idCardLast == idCardY[idCardMod]) {
                                //alert("恭喜通过验证啦！");
                                return true;
                            } else {
                                //alert("身份证号码错误！");
                                Backend.api.toastr.error("身份证号码验证错误,请修改！");
                                return false;
                            }
                        }
                    }
                } else {
                    //alert("身份证格式不正确!");
                    Backend.api.toastr.error("身份证格式不正确!");
                    return false;
                }
            },
            showMonthFirstDay: function () {
                var Nowdate = new Date();
                var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1);
                M = Number(MonthFirstDay.getMonth()) + 1
                return MonthFirstDay.getFullYear() + "-" + M + "-" + MonthFirstDay.getDate() + ' 00:00:00';
            },
            showMonthLastDay: function () {
                var Nowdate = new Date();
                var MonthNextFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth() + 1, 1);
                var MonthLastDay = new Date(MonthNextFirstDay - 86400000);
                M = Number(MonthLastDay.getMonth()) + 1
                return MonthLastDay.getFullYear() + "-" + M + "-" + MonthLastDay.getDate() + ' 23:59:59';
            },
            defaultdate2: function () {
                //2019-07-01 00:00:00 - 2019-07-31 23:59:59
                var MonthFirstDay = Controller.api.showMonthFirstDay();
                var MonthLastDay = Controller.api.showMonthLastDay();

                return MonthFirstDay + " - " + MonthLastDay;
            },
            formatter: {//渲染的方法
                zcstatus: function (value, row, index) {
                    if (row.zczt != '已办理') {
                        return [
                            '<a href="javascript:;" class="zcstatus text-danger" data-id= "' + row.id + '" > ' + row.zczt + '</a>'
                        ].join("")

                    }  else {
                        return [
                            '<a href="javascript:;" class="zcstatus" data-id= "' + row.id + '" > ' + row.zczt + '</a>'
                        ].join("")
                    }
                },
                sbstatus: function (value, row, index) {
                    if (row.sbzt != '已办理') {
                        return [
                            '<a href="javascript:;" class="sbstatus text-danger" data-id= "' + row.id + '" > ' + row.sbzt + '</a>'
                        ].join("")

                    }  else {
                        return [
                            '<a href="javascript:;" class="sbstatus" data-id= "' + row.id + '" > ' + row.sbzt + '</a>'
                        ].join("")
                    }
                },
            }
        }
    };
    return Controller;
});